package dormouse.vfs.ui;

import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.FileSystemManager;
import org.apache.commons.vfs2.VFS;

public class RootFileFactory {
	
	public static final int HOME = 0;
	public static final int ROOT = 1;
	public static final int DOCUMENTS = 2;
	public static final int MUSIC = 3;
	public static final int PICTURES = 4;
	public static final int MOVIES = 5;
	public static final int EBOOKS = 6;
	public static final int TEMP = 7;
	
	public static final int[] ROOTS = new int[]{HOME, ROOT, DOCUMENTS, MUSIC, PICTURES, MOVIES, EBOOKS, TEMP};
	
	private static FileObject home = null;
	private static FileObject root = null;
	private static FileObject documents = null;
	private static FileObject music = null;
	private static FileObject movies = null;
	private static FileObject pictures = null;
	private static FileObject ebooks = null;
	private static FileObject temp = null;
	private static FileObject trash = null;
	
	static {
		try {
			String osName = System.getProperty("os.name");
			String os = null;
			if (osName.indexOf("Mac") != -1){
				os = "Mac";
			}else if (osName.indexOf("Windows") != -1){
				os = "Windows";
			}else if (osName.indexOf("Linux") != -1){
				os = "Linux";
			}
			
			if (os ==  null){
				throw new FileSystemException("The os property was null");
			}

			
			FileSystemManager mgr = VFS.getManager();
			home = mgr.resolveFile(System.getProperty("user.home"));
			if (os.equals("Mac")){
				documents = home.getChild("Documents");
				music = home.getChild("Music");
				pictures = home.getChild("Pictures");
				movies = home.getChild("Movies");
				ebooks = home.getChild("E-Books");
			}else if (os.equals("Windows")){
				documents = home.getChild("My Documents");
				music = documents.getChild("My Music");
				movies = documents.getChild("My Videos");
				pictures = documents.getChild("My Pictures");
				ebooks = documents.getChild("My E-Books");
			}else if (os.equals("Linux")){
				documents = home.getChild("Documents");
				music = home.getChild("Music");
				movies = home.getChild("Movies");
				pictures = home.getChild("Pictures");
				ebooks = home.getChild("EBooks");
			}
			temp = mgr.resolveFile(System.getProperty("java.io.tmpdir"));
			
		} catch (FileSystemException e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * This method is responsible for creating an instance of a root directory.
	 * These root directories include the HOME, the file system root, as well
	 * as document-, movies-, pictures-, and e-books-roots.
	 * 
	 * @param rootType  One of the root constants defined in this class.
	 * @return  A FileObject representing the root directory.
	 * @throws FileSystemException 
	 */
	public static FileObject getInstance(int rootType) throws FileSystemException{
		FileObject inst = null;
		
		switch (rootType){
		case HOME:{
			inst = home;
			break;
		}
		case ROOT:{
			inst = root;
			break;
		}
		case DOCUMENTS:{
			inst = documents;
			break;
		}
		case MUSIC:{
			inst = music;
			break;
		}
		
		case PICTURES:{
			inst = pictures;
			break;
		}
		case TEMP:{
			inst = temp;
			break;
		}
		default:{
			throw new FileSystemException("Unsupported root type: " + rootType);
			
		}
		}
		
		
		if (!inst.exists()){
			inst.createFolder();
		}
		return inst;
	}
	
	/**
	 * 
	 * @return
	 */
	public static FileObject[] getRoots(){
		FileObject[] roots = null;
		
		return roots;
	}

}
